/**
 * Created by admin on 2017/9/5.
 */
define(['app', 'filters/filters'], function(app) {
    app.register.controller('reviewtheTopic',['$scope','$state','$stateParams','$rootScope','$Ajax','$http', function($scope, $state, $stateParams, $rootScope,$Ajax,$http) {
        var http = app._baseUrl;
        $scope.obj = $stateParams.obj;
        $scope.obj = JSON.parse($scope.obj);//获取到用户填写信息
        $scope.load = {
            _getInitial: function() { //专业类别和专业

            },
            init: function() {
                this._getInitial && this._getInitial();
            }
        };
        $scope.load.init && $scope.load.init();

        //   ------------------------------------------------温馨提示弹窗
        $scope.popupModal = {
            tipModal: false,
            hide: function () {
                this.tipModal = false;
            },
            show: function () {
                this.tipModal = true;
            }
        };

        // ---------------------------------------------答题

        $scope.getQues = {
            close:function(){
                window.onbeforeunload = null;
                var url = $state.href('app.informationEntry',{token:$stateParams.token});//跳转到登录页面
                window.open(url,'_self');
            },
            question_level:'',
            ispushz_ans:true,
            tipShowzy:false,
            showfinish:false,
            btnshowText:'下一题',
            isfinish:false,
            type:'单',
            token:$stateParams.token,
            cate_id:$scope.obj.cate_id,
            quesshowIndex:'1',
            quesshowTitle:'',
            quesnextIndex:'',
            quesshowLi:{},
            ability_check_question_id:'',
            timeRemain:'20',//剩余时间
            z_ques:{},
            isAjax:true,
            z_answer:'',
            z_answerAll:[],
            g_answerAll:[],
            z_http:false,
            interval:{},
            img:'',
            getFirstques:function(){
                var url = http + '/Api/AbilityCheck/getMiddleQuestionList';
                var data = {
                    cate_id:this.cate_id
                }
                $Ajax.postData(url,data).then(function(res){
                    if(res.data.success){
                        $scope.getQues.isAjax = true;
                        $scope.getQues.z_ques = res.data.data.question_list;
                        $scope.getQues.quesshowLi = $scope.getQues.z_ques[0];
                        $scope.getQues.showInfoques();
                        $scope.getQues.timers();
                    }
                })
            },
            showInfoques:function(){
                if(this.z_ques == ''){//这个版本中级题目做完之后没有高级或初级题会跳转到最后完成页面 暂时这样写 要改后期改。
                    this.showfinish = true;
                    return false;
                }
                this.quesshowTitle = this.quesshowLi.title;
                this.type = this.quesshowLi.type==0?'单':'多';
                if(this.quesshowLi.type==0){
                    this.timeRemain = '20';
                }else{
                    this.timeRemain = '30';
                }
                this.ability_check_question_id = this.quesshowLi.ability_check_question_id;
            },
            clickLi:function(index){//选项点击
                if(this.type == '单'){//单选题
                    if($('.questionr_con li').eq(index).hasClass('active')){
                        $('.questionr_con li').eq(index).removeClass('active');
                    }else{
                        $('.questionr_con li').removeClass('active');
                        $('.questionr_con li').eq(index).addClass('active');
                    }
                }else{
                    $('.questionr_con li').eq(index).hasClass('active_duo_active') ? $('.questionr_con li').eq(index).removeClass('active_duo_active') : $('.questionr_con li').eq(index).addClass('active_duo_active');
                }
            },
            answerThis:function(){//获取本题选项
                if(this.type=='单'){//如果本题是单选
                        for(var i=0;i<4;i++){
                            if($('.questionr_con li').eq(i).hasClass('active')){
                                return this.toXuanX(i);
                            }
                        }
                        return '';
                }else{
                        var strAnswer = '';
                        for(var i=0;i<5;i++){
                            if($('.questionr_con li').eq(i).hasClass('active_duo_active')){
                                if(strAnswer==''){
                                    strAnswer = this.toXuanX(i);
                                }else{
                                    strAnswer = strAnswer +','+this.toXuanX(i);
                                }
                            }
                        }
                        return strAnswer;

                }
            },
            toXuanX:function(index){
                if(index==0){
                    return 'A';
                }
                if(index==1){
                    return 'B';
                }
                if(index==2){
                    return 'C';
                }
                if(index==3){
                    return 'D';
                }
                if(index==4){
                    return 'E'
                }
            },
            clickBtn:function(){//下一题点击
                //判断是否有选择---后续加吧
                if(($('.questionr_con li').hasClass('active_duo_active')==false&&this.type=='多'&&this.timeRemain!='00') || ($('.questionr_con li').hasClass('active')==false&&this.type=='单')&&this.timeRemain!='00'){
                    // this.isAjax = false;
                    $scope.sec = 5;
                    var interval = setInterval(function() {//设置定时器
                        if($scope.sec > 0) {
                            $scope.sec--;
                            $scope.getQues.tipShowzy = true;//提示显示
                            $('.tool_zy').fadeIn(600);
                        } else {
                            clearInterval(interval);
                            $scope.getQues.tipShowzy = true;
                            $('.tool_zy').fadeOut(600);
                        }
                        $scope.$digest();//脏数据检验
                    }, 500);
                    return false;
                }
                if(this.isAjax){
                   this.isAjax = false;
                   //--图片自动关闭开始
                    if($scope.img_big==true){
                        $scope.img_click(this.img);
                    }
                   //--图片自动关闭结束
                   clearInterval(this.interval);
                   // this.timeRemain = '15';
                   // this.timers();
                   //获取本题选项
                   this.z_answer = this.answerThis();
                   //记录本题答案
                   var ans = {
                       ability_check_question_id:this.ability_check_question_id,
                       s_answer:this.z_answer
                   };
                   if(this.ispushz_ans){
                       this.z_answerAll.push(ans);
                   }else{
                       this.g_answerAll.push(ans);
                   }
                    if(this.isfinish){
                        //请求提交全部答案
                        this.allRequest();
                        return false;
                    }
                   if(this.z_http){
                       this.ispushz_ans = false;//后期请求的题目另存
                       //请求高级或初级题目
                       this.z_http = false;
                        //请求接下来初高级试题
                       this.gRcques();
                   }else{
                       this.nextQues();
                   }

               }

            },
            gRcques:function () {//请求高级或者初级题目
                var url = http + '/Api/AbilityCheck/submitMiddleAndGetNextQuestionList';
                var data = {
                    answer_list_step_1:JSON.stringify(this.z_answerAll),
                    token:this.token
                };
                // console.log(this.z_answerAll);
                $Ajax.postData(url,data).then(function(res){
                    if(res.data.success){
                        $scope.getQues.z_ques = res.data.data.question_list;
                        $scope.getQues.question_level = res.data.data.question_level;
                        $scope.getQues.quesshowLi = $scope.getQues.z_ques[0];
                        $scope.getQues.showInfoques();
                        $scope.getQues.timers();
                        $scope.getQues.quesnextIndex = 1;
                        $scope.getQues.quesshowIndex ++;
                        $scope.getQues.isAjax = true;
                        $('.questionr_con li').removeClass('active');
                        $('.questionr_con li').removeClass('active_duo_active');
                    }
                });
            },
            nextQues:function(){//显示后面的题目
                this.isAjax = true;
                $('.questionr_con li').removeClass('active');
                $('.questionr_con li').removeClass('active_duo_active');
                if(this.quesnextIndex == ''){
                    if(this.quesshowIndex < this.z_ques.length){
                        this.quesshowIndex ++;
                        this.quesshowLi = this.z_ques[this.quesshowIndex-1];
                        this.showInfoques();
                        this.timers();
                        if(this.quesshowIndex == this.z_ques.length){
                            this.z_http = true;
                        }
                    }
                }else{
                    if(this.quesnextIndex < this.z_ques.length){
                        this.quesshowIndex ++;
                        this.quesnextIndex ++;
                        this.quesshowLi = this.z_ques[this.quesnextIndex-1];
                        this.showInfoques();
                        this.timers();
                        if(this.quesnextIndex == this.z_ques.length){
                            this.btnshowText = '完成';
                            this.isfinish = true;
                        }
                    }else{
                        // this.showfinish = true;
                    }
                }



            },
            timers:function(){
                var sec = this.timeRemain;
                this.interval = setInterval(function(){
                    if(sec > 0){
                        sec--;
                        if(sec < 9 || sec == 9){
                            sec = '0'+sec;
                        }
                        $scope.getQues.timeRemain = sec.toString();
                    }else{
                        $scope.getQues.clickBtn();
                    }
                    $scope.$digest();
                },1000);
            },
            allRequest:function(){
                var url = http + '/Api/AbilityCheck/submitAllAnswer';
                var data = {
                    answer_list_step_1:JSON.stringify(this.z_answerAll),
                    answer_list_step_2:JSON.stringify(this.g_answerAll),
                    user_info:JSON.stringify($scope.obj),
                    question_level:this.question_level,
                    token:this.token
                };
                $Ajax.postData(url,data).then(function(res){

                });
                this.showfinish = true;
            }
        };
        $scope.getQues.getFirstques();

        //-------------------------------------------图片放大
        //图片放大
        $scope.img_big=false;
        $scope.img_click = function(img){
            //将图片路径保存在点击下一题按钮模板中开始
            $scope.getQues.img = img;
            //将图片路径保存在点击下一题按钮模板中结束
            $scope.img_big=!$scope.img_big;
            $scope.scaleImg=img;
            var imgObj = new Image();
            imgObj.src = img; //若是jq，则直接将此代码换成 imgObj.src = _test.attr("src"); 即可。
            var k = document.body.clientWidth/2-imgObj.width/2;
            var v = document.body.clientHeight/2-imgObj.height/2;
            $(".bigImgBox").css("left",k+'px');
            $(".bigImgBox").css("top",v+'px');
            $scope.obj_img = {
                left:k,
                top:v,
                width:imgObj.width,
                height:imgObj.height
            }
            $(".bigImgBox").css("left",$scope.obj_img.left+'px');
            $(".bigImgBox").css("top",$scope.obj_img.top+'px');
            $('.bigImgBox .bigImg').css({'width':$scope.obj_img.width,"height":$scope.obj_img.height});
        }
        //图片放大结束
        var divImg = document.getElementsByClassName('bigImgBox');
        divImg[0].addEventListener('mousewheel',function(e){
            var imgWidth = $('.bigImgBox .bigImg').width();
            var imgHeight = $('.bigImgBox .bigImg').height();
            var imgLeft = divImg[0].offsetLeft;
            var imgTop = divImg[0].offsetTop;
            if(e.deltaY > 0 || e.wheelDelta < 0){
                imgLeft = imgLeft-imgWidth/20;
                imgTop = imgTop-imgHeight/20;
                imgWidth = imgWidth+imgWidth/10;
                imgHeight = imgHeight+imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
            if(e.deltaY < 0|| e.wheelDelta > 0){
                imgLeft = imgLeft+imgWidth/20;
                imgTop = imgTop+imgHeight/20;
                imgWidth = imgWidth-imgWidth/10;
                imgHeight = imgHeight-imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
        });
        divImg[0].addEventListener('mousewheel',function(e){
            var imgWidth = $('.bigImgBox .bigImg').width();
            var imgHeight = $('.bigImgBox .bigImg').height();
            var imgLeft = divImg[0].offsetLeft;
            var imgTop = divImg[0].offsetTop;
            if(e.deltaY > 0 || e.wheelDelta < 0){
                imgLeft = imgLeft-imgWidth/20;
                imgTop = imgTop-imgHeight/20;
                imgWidth = imgWidth+imgWidth/10;
                imgHeight = imgHeight+imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop});
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
            if(e.deltaY < 0|| e.wheelDelta > 0){
                imgLeft = imgLeft+imgWidth/20;
                imgTop = imgTop+imgHeight/20;
                imgWidth = imgWidth-imgWidth/10;
                imgHeight = imgHeight-imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
        });
    //    ----------------------------------------中途退出弹窗
        window.onbeforeunload = function(even) {

            return '刷新或关闭后将不保留做题记录'; // 可以阻止关闭
        };
    }]);
})